package it.univpm.diiga.memorysim.views;

import java.util.ArrayList;

import it.univpm.diiga.memorysim.data.DataPlugin;
import it.univpm.diiga.memorysim.data.Situation;

import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.graphics.Rectangle;

public class OutputSimulationHDD extends ViewPart {

	public static final String ID = "it.univpm.diiga.memorysim.views.OutputSimulationHDD"; 
	private Composite top = null;
	private ScrolledComposite sc1 = null,quad=null;
	private static Composite c1;
	private static int inv_coeff=20; // coeff^-1 con coeff=0.05;
	private static int dim_all;
	private static int row_min=10;
	private static int row_max=700;
	private int spessore=30;
	private static int inc=33;
	private static int[] spaces=new int[]{0};
	//                     color_space: | indice | indice_elemento_tabella | valore_colore
	//private static Hashtable<Integer, int[]> color_space=new Hashtable<Integer, int[]>();
	private static int[] color_space=new int[]{0};
	private static int n_row=1;
	private static int area;
	private int selection_table=0;
	
	public void createPartControl(Composite parent) {
		final Shell shell = getViewSite().getShell();
		top = new Composite(parent, SWT.NONE);
		top.setLayout(null);
		quadratino(shell);
		Label l0 = new Label(top,SWT.NONE);
		l0.setText("Area Selezionata");
		l0.setBounds(new Rectangle(50, 5, 200, 15));
		Label l03 = new Label(top,SWT.NONE);
		l03.setText("Hard Disk "+DataPlugin.hdddimensiondata+"GB");
		l03.setBounds(new Rectangle(20, 29, 150, 15));
		createSc1();
		drawHDD(shell);
		sc1.setContent(c1);
		sc1.setExpandHorizontal(true);
		sc1.setExpandVertical(true);
		update(selection_table);
		sc1.setMinSize(10, area);	
		
	}

	private void quadratino(final Shell shell) {
		
		quad = new ScrolledComposite(top, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		quad.setLayout(new GridLayout());
		quad.setBounds(new Rectangle(20, 5, 20, 15));
		quad.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));

	}

	private void createSc1() {
		sc1 = new ScrolledComposite(top, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		sc1.setLayout(new GridLayout());
		sc1.setBounds(new Rectangle(20, 50, 775, 324));
		createC1();
	}


	private void createC1() {
		GridLayout gridLayout31 = new GridLayout();
		gridLayout31.numColumns = 10;
		gridLayout31.makeColumnsEqualWidth = false;
		c1 = new Composite(sc1, SWT.NONE);
		c1.setLayout(gridLayout31);
	}

	
		
	private void drawHDD(final Shell shell) {
//c1 -> memoria centrale
		
		c1.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
						int fine = 0;
						int carry=0;
						//Struttura base
						/*
						for(int i=1;i<=n_row;i++){
							event.gc.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
							event.gc.setLineWidth(spessore);
							event.gc.drawLine(row_min, i*inc, row_max, i*inc);
						}
						*/
						//PROVA HDD
						int devo_scrivere=dim_all;
						int start_y=inc;
						while(devo_scrivere>0){
							
							event.gc.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
							event.gc.setLineWidth(spessore);
							if(devo_scrivere>row_max){
								devo_scrivere=devo_scrivere-(row_max-row_min);
								event.gc.drawLine(row_min, start_y, row_max, start_y);
							}
							else{
								event.gc.drawLine(row_min, start_y, row_min+devo_scrivere, start_y);
								devo_scrivere=0;
							}
							
							start_y+=inc;
						}
						//FINE PROVA
						start_y=inc;
						int inizio = row_min;
						int l=0;
						carry=0;
						while(l<spaces.length){
							if(carry==0){
								event.gc.setForeground(shell.getDisplay().getSystemColor(color_space[l]));
								fine=inizio+spaces[l];
							}else{
								l=l-1;
								event.gc.setForeground(shell.getDisplay().getSystemColor(color_space[l]));
								fine=inizio+carry;
								carry=0;
								}
							event.gc.setLineWidth(spessore);
							if(fine>=row_max){
								carry=fine-row_max;
								fine=row_max;
								event.gc.drawLine(inizio, start_y, fine, start_y);
								start_y+=inc;
								inizio=row_min;
							}else{
								event.gc.drawLine(inizio, start_y, fine, start_y);
								inizio=fine;
							}
							l++;
						}
			}

		});
	}
	
	private static int colore_precedente=0;
	private static String selezione_precedente="";
	
	public static void selectionTable(String path_select, int id_select){
		int id_sel=findIdByPath(path_select,id_select);
		if(id_sel>=0){
			int color_sel=SWT.COLOR_GREEN;
			if(!selezione_precedente.equals("")){
				int id_setNew=findIdByPath(selezione_precedente,id_select);
				if(id_setNew>=0) color_space[id_setNew]=colore_precedente;
				else{
					colore_precedente=0;
					selezione_precedente="";
				}	
			}
			selezione_precedente=path_select;
				colore_precedente=color_space[id_sel];
				color_space[id_sel]=color_sel;
			}
			c1.redraw();
	}
	
	private static int findIdByPath(String path, int row_select) {
		int id_sel=-1;
		try{
			for(int i=0;i<Situation.getHDDlistFiles().get(row_select).size();i++){
				if(Situation.getHDDlistFiles().get(row_select).get(i).equals(path)) {
					id_sel=i;
					}
			}
		}catch(Exception e){}
		return id_sel;
	}

	public static void update(int selection_table){
		try{
			// spazio_occupato.
			int tot_pixel=0;
			ArrayList<Integer> stato1 = Situation.getHDDstate().get(selection_table);
			ArrayList<String> files1 = Situation.getHDDlistFiles().get(selection_table);
			if(stato1.size()>0){
				spaces= new int[stato1.size()+1];
				spaces[spaces.length-1]=0;
			}
			if(files1.size()>0){
				color_space= new int[files1.size()+1];
				color_space[color_space.length-1]=SWT.COLOR_DARK_GRAY;
			}
			for(int i=0,j=3;i<stato1.size();i++,j++){
				if(stato1.get(i)<20) {
					tot_pixel++;
					spaces[i]=1;
					
				}else{
					spaces[i]=stato1.get(i)/inv_coeff;
					tot_pixel+=spaces[i];
					}
				if(j>=14) j=3;
				if(j==SWT.COLOR_GREEN) ++j;
				if(files1.get(i).equals("-1")) color_space[i]=SWT.COLOR_DARK_GRAY;
				else color_space[i]=j;
				
	
			}		
			//for(int i : spaces) System.out.println(i);
			int hdd_pixel=(DataPlugin.hdddimensiondata*1024)/inv_coeff;
			
				if(tot_pixel>hdd_pixel) dim_all=tot_pixel;
					else dim_all=hdd_pixel;
			double n_row1=dim_all/(row_max-row_min);
			n_row=dim_all/(row_max-row_min);
			if(n_row1>n_row) n_row++;
			area=100+(n_row*inc);
			c1.redraw();
			System.out.println("hdd_pixel: "+hdd_pixel);
			System.out.println("dim_all: "+dim_all);
			System.out.println("n_row: "+n_row);
			System.out.println("n_row1: "+n_row1);
			for(int a : spaces)System.out.println("dimensioni file: "+a);
	}catch(Exception e){}
	}
	
	@Override
	public void setFocus() { }

}

